package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.io.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Action: Sent by USR/ADM to 'forcibly' unlock a Proposal which has been either locked
 * accidentaly e.g. when a network error has occurred after locking for edit.
 * NOTE: This TR will NOT be available in this unsafe form to normal users on release
 * @author S.N.Fraser
 * $Id: FORCE_UNLOCK_PROPOSALImpl.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 */
public class FORCE_UNLOCK_PROPOSALImpl extends TransactionImpl {

    /** The path used to locate the Proposal in the OSS database. */
    protected Path proposalPath;

    public FORCE_UNLOCK_PROPOSALImpl(FORCE_UNLOCK_PROPOSAL request) {
  	super(request);
  	this.proposalPath = request.getProposalPath();
    }


    /**
     * Forcibly unlocks the Proposal specified  
     */
    public TRANSACTION_DONE exec() { 
	
	Proposal proposal = null;
	
	Transaction t = null;
	try {
	    t = Transaction.current();
	    
	    // Locate the Proposal record from its path expression.
	    try {
		proposal = OSS_Utilities.fetchProposal(proposalPath);
	    } catch (IOException iox) {
		logger.log(1, CLASS, id, "exec",
			   "Proposal: "+proposalPath.toString()+
			   " Error fetching from Database: "+iox);
		return error(TRANSACTION.DATABASE_READ_ERROR,			   
			     "Proposal: "+proposalPath.toString()+
			     " Error reading from database: "+iox);
	    }		
	    
	    // Check it exists.
	    if (proposal == null) {		
		logger.log(1, CLASS, id, "exec",
			   "Proposal: "+proposalPath.toString()+" Not in database.");		
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
			     "Could not find Proposal: "
			     +proposalPath.toString());
	    } 
	    
	    // Make sure the key will unlock the Proposal.
	    if (!proposal.isLocked() && !proposal.canLock()) {
		// The Proposal isn't locked anyway.
		logger.log(1, CLASS, id, "exec",
			   "Proposal: "+proposalPath.toString()+
			   " Attempt to unlock Proposal which has not been locked:");
		return error(TRANSACTION.DATABASE_OBJECT_NOT_LOCKED,
			     "Attempt to unlock Proposal which has not been locked: "
			     +proposalPath.toString());
	    }
	    
	    // Ok it is locked OR its subtree is locked somewhere
	    int key = proposal.getLock();
	    proposal.forceUnLock();
	
	  
	    // Store it
	    try {
		OSS_Utilities.storeProposal(proposal);
	    } catch (IOException ix) {
		logger.log(1, CLASS, id, "exec",
			   "Proposal ["+proposal.getFullPath()+
			   "] Unable to Store: "+ix);
		return error(TRANSACTION.DATABASE_UPDATE_ERROR, 
			     "Proposal ["+proposal.getFullPath()+
			     "] Unable to Store: "+ix);
	    }	    	   

	} finally {
	    t.commit();
	} 
	
	FORCE_UNLOCK_PROPOSAL_DONE done = new FORCE_UNLOCK_PROPOSAL_DONE(id); 
	done.setSuccessful(true);
	return done;		    
    }
    
}

/** $Log: FORCE_UNLOCK_PROPOSALImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2001/02/23 18:51:14  snf
/** Initial revision
/** */
